﻿Imports CRM.BusinessLayer
Imports System.Data
Imports System.ComponentModel

Partial Public Class frmCompanyDetails
    Public isCompanyDetailAdded As Boolean = False
    Dim myCompany As Company.CompanyInfo = Nothing
    Private Sub CompanyDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        myCompany = Company.GetCompany(GlobalCRM.CurrentCompanyID)

        With Me.drpIndustry
            .DisplayMemberPath = "Industry"
            .SelectedValuePath = "Industry_ID"
            .ItemsSource = CType(Admin.IndustryList, IListSource).GetList
        End With

        With Me.drpSize
            .DisplayMemberPath = "description"
            .SelectedValuePath = "companysize_id"
            .ItemsSource = CType(CommonDataFunctions.CompanySizeList, IListSource).GetList
        End With

        With Me.drpWhereHeard
            .DisplayMemberPath = "wherehear_text"
            .SelectedValuePath = "wherehear_id"
            .ItemsSource = CType(CommonDataFunctions.WhereHearList, IListSource).GetList
        End With

        With Me.drpBranch
            .DisplayMemberPath = "Branch_Name"
            .SelectedValuePath = "Branch_ID"
            .ItemsSource = CType(CommonDataFunctions.BranchList(), IListSource).GetList
            .SelectedValue = CommonDataFunctions.GetLoggedOnConsultantBranchID
        End With

        If Security._SecurityLevel <> SecurityLevel.AdminUser Then
            Me.drpBranch.IsEnabled = False
        End If

        With Me.drpAccountMan
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            '.ItemsSource = CType(CommonDataFunctions.ConsultantList(, , myCompany.CompanyCoordinatorID), IListSource).GetList
            .ItemsSource = CType(CommonDataFunctions.ConsultantsList(-1), IListSource).GetList
        End With

        With Me.drpAccountCW
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantsList(-1), IListSource).GetList
        End With

        With Me.drpCoordinator
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantsList(-1), IListSource).GetList
        End With

        With Me.drpAccType
            .DisplayMemberPath = "accounttype_name"
            .SelectedValuePath = "accounttype_id"
            .ItemsSource = CType(CommonDataFunctions.AccountTypeList, IListSource).GetList
        End With

        With Me.drpCompanyStatus
            .DisplayMemberPath = "companystatus_name"
            .SelectedValuePath = "companystatus_id"
            .ItemsSource = CType(CommonDataFunctions.CompanyStatusList, IListSource).GetList
        End With

        With myCompany
            Me.lblID.Content = .CompanyID
            Me.txtCompanyName.Text = .CompanyName
            Me.drpIndustry.SelectedValue = .IndustryId
            Me.drpSize.SelectedValue = .CompanySizeId
            Me.txtWebsite.Text = .Website
            Me.drpWhereHeard.Text = .WhereHeard
            Me.drpAccountMan.SelectedValue = .AccountManagerId
            Me.drpAccountCW.SelectedValue = .DefaultCaseWorkerId
            Me.drpCoordinator.SelectedValue = .CompanyCoordinatorID
            Me.drpAccType.SelectedValue = .AccountTypeId
            Me.drpCompanyStatus.SelectedValue = .CompanyStatusId
            Me.drpBranch.SelectedValue = CommonDataFunctions.GetLoggedOnConsultantBranchID
        End With

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If Me.txtCompanyName.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Company Name cannot be blank", CommonDialog.CustomDialogIcons.Information, "Company Name")
            Exit Sub
        End If

        If Me.drpWhereHeard.Text.Trim = String.Empty Then
            If MsgBox("How the person heard about Global Visas could not be added to the database. Do you wish to continue?", MsgBoxStyle.YesNo, "Error") = MsgBoxResult.No Then
                Me.drpWhereHeard.Focus()
                Exit Sub
            End If
            'MsgBox("WhereHear cannot be blank", MsgBoxStyle.Exclamation, "Information")
            'Exit Sub
        End If

        If Me.drpIndustry.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Industry type cannot be blank", CommonDialog.CustomDialogIcons.Information, "Industry")
            Exit Sub
        End If

        If Me.drpSize.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Company size cannot be blank", CommonDialog.CustomDialogIcons.Information, "Company")
            Exit Sub
        End If

        If Me.drpAccountMan.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Account Manager cannot be blank (set to unassigned if none)", CommonDialog.CustomDialogIcons.Information, "Account Manager")
            Exit Sub
        End If

        If Me.drpAccountCW.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Caseworker cannot be blank (set to unassigned if none)", CommonDialog.CustomDialogIcons.Information, "Case Worker")
            Exit Sub
        End If

        If Me.drpCoordinator.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Coordinator cannot be blank (set to unassigned if none)", CommonDialog.CustomDialogIcons.Information, "Coordinator")
            Exit Sub
        End If

        If Me.drpAccType.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Company account type cannot be blank", CommonDialog.CustomDialogIcons.Information, "Account")
            Exit Sub
        End If

        If Me.drpCompanyStatus.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Company status cannot be blank", CommonDialog.CustomDialogIcons.Information, "Status")
            Exit Sub
        End If

        Dim OldInfo As Company.CompanyInfo = Company.GetCompany(GlobalCRM.CurrentCompanyID)
        Dim NewInfo As New Company.CompanyInfo

        With NewInfo
            .CompanyID = GlobalCRM.CurrentCompanyID
            .CompanyName = Me.txtCompanyName.Text.Trim
            .WhereHeard = Me.drpWhereHeard.Text.Trim
            .IndustryId = Me.drpIndustry.SelectedValue
            .IndustryName = Me.drpIndustry.Text.Trim
            .CompanySizeId = Me.drpSize.SelectedValue
            .CompanySize = Me.drpSize.Text.Trim
            .AccountManagerId = Me.drpAccountMan.SelectedValue
            .AccountManager = Me.drpAccountMan.Text.Trim
            .DefaultCaseWorkerId = Me.drpAccountCW.SelectedValue
            .DefaultCaseWorker = Me.drpAccountCW.Text
            .AccountTypeId = Me.drpAccType.SelectedValue
            .AccountType = Me.drpAccType.Text.Trim
            .CompanyStatusId = Me.drpCompanyStatus.SelectedValue
            .CompanyStatus = Me.drpCompanyStatus.Text.Trim
            .CompanyCoordinatorID = Me.drpCoordinator.SelectedValue
            .Website = Me.txtWebsite.Text.Trim
            .WhereHeard = Me.drpWhereHeard.Text.Trim
        End With

        If Company.AddCompany(NewInfo, OldInfo) Then
            isCompanyDetailAdded = True
            CommonFuncs.DisplayMessage("Company details added successfully !", CommonDialog.CustomDialogIcons.Information, "Account")
            Me.Close()
        End If

    End Sub

    Private Sub drpBranch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpBranch.SelectionChanged

        With Me.drpAccountMan
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(, , , drpBranch.SelectedValue), IListSource).GetList
            .SelectedValue = myCompany.AccountManagerId
        End With

        With Me.drpAccountCW
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(, , , drpBranch.SelectedValue), IListSource).GetList
            .SelectedValue = myCompany.DefaultCaseWorkerId
        End With

        With Me.drpCoordinator
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(, , , drpBranch.SelectedValue), IListSource).GetList
            .SelectedValue = myCompany.CompanyCoordinatorID
        End With

    End Sub

End Class
